// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.commands;

import java.util.List;

import org.dengues.core.components.ENodeParameterName;
import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.process.ICompProcess;
import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.models.CompNodeLabel;
import org.dengues.designer.ui.process.models.CompProcess;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class CompNodeLabelTextChangeCommand extends AbstractGEFCommand {

    private final CompNodeLabel label;

    private final String newText;

    private String oldText;

    /**
     * Qiang.Zhang.Adolf@gmail.com CompNodeLabelTextChangeCommand constructor comment.
     * 
     * @param label
     * @param newText
     */
    public CompNodeLabelTextChangeCommand(CompNodeLabel label, String newText) {
        setLabel(Messages.getString("CompNodeLabelTextChangeCommand.change")); //$NON-NLS-1$
        this.label = label;
        this.newText = newText;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#canExecute()
     */
    @Override
    public boolean canExecute() {
        CompNode parent = label.getParentNode();
        if (parent != null) {
            ICompProcess compProcess = parent.getCompProcess();
            if (compProcess instanceof CompProcess) {
                CompProcess process = (CompProcess) compProcess;
                List<CompNodeLabel> compNodeLabels = process.getCompNodeLabels();
                for (CompNodeLabel compNodeLabel : compNodeLabels) {
                    if (compNodeLabel.getNodeLabel().equals(newText)) {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public void execute() {
        oldText = label.getNodeLabel();
        label.setNodeLabel(newText);
        NodePropertyParameter param = label.getParentNode().getElementParameter(ENodeParameterName.UNIQUE_NAME.getName());
        if (param != null) {
            param.setValue(newText);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#undo()
     */
    @Override
    public void undo() {
        label.setNodeLabel(oldText);
        NodePropertyParameter param = label.getParentNode().getElementParameter(ENodeParameterName.UNIQUE_NAME.getName());
        if (param != null) {
            param.setValue(oldText);
        }
    }
}
